import { ContentBlocks } from "@renderer/components/business/markdown/content-blocks";
import { MarkdownRenderer } from "@renderer/components/business/markdown/markdown-renderer";
import { SequentialContentRenderer } from "@renderer/components/business/sequential-content";
import { useContentBlocks } from "@renderer/hooks/use-content-blocks";
import { useSequentialContentUpdates } from "@renderer/hooks/use-sequential-content-updates";
import type { ChatSettings } from "@shared/types/chat-settings";
import { MessageAttachments } from "../message-attachments";

interface MessageContentProps {
  messageId: string;
  threadId: string;
  content: string;
  settings: ChatSettings;
  toolCalls?: string; // JSON string of ToolCallResult[] - legacy format
  contentBlocks?: string; // JSON string of SequentialMessageContent - new MCP format
  hasSequentialContent?: boolean;
}

/**
 * MessageContent handles the rendering of assistant message content with support for both
 * legacy tool calling and the new MCP reflection->action sequential content format.
 *
 * Rendering Logic:
 * - If hasSequentialContent is true: renders reflection and tool call blocks in chronological order
 * - Otherwise: renders legacy tool calls followed by standard markdown content
 *
 * This dual-mode approach maintains backwards compatibility while supporting the enhanced
 * MCP workflow for more thoughtful AI responses.
 */
export function MessageContent({
  messageId,
  threadId,
  content,
  settings,
  contentBlocks,
  hasSequentialContent,
}: MessageContentProps) {
  const { cleanContent } = useContentBlocks(content);

  // Hook for new sequential content format - supports MCP reflection->action flow
  const { blocks, isSequential } = useSequentialContentUpdates(
    messageId,
    contentBlocks,
    hasSequentialContent,
  );

  return (
    <div className="w-full min-w-0">
      <MessageAttachments messageId={messageId} className="mb-2" />

      {/* New MCP Sequential Content Format */}
      {isSequential && blocks.length > 0 ? (
        <SequentialContentRenderer
          messageId={messageId}
          threadId={threadId}
          contentBlocks={blocks}
          settings={settings}
          className="mb-3"
        />
      ) : (
        <>
          {/* Legacy Tool Calls Display - for backward compatibility */}
          {/* {toolCallResults.length > 0 && (
            <ToolCallsList
              toolCalls={toolCallResults}
              threadId={threadId}
              messageId={messageId}
              className="mb-3"
            />
          )} */}

          <ContentBlocks
            content={content}
            messageId={messageId}
            settings={settings}
          />

          {/* Standard Markdown Content Rendering */}
          {cleanContent && (
            <div className="overflow-wrap-anywhere w-full break-words break-all text-fg">
              <MarkdownRenderer settings={settings}>
                {cleanContent}
              </MarkdownRenderer>
            </div>
          )}
        </>
      )}
    </div>
  );
}
